﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_QuestionManager_SelectQuestion : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerPaperInfo manager = new ManagerPaperInfo();
    //ManagerQuestionTypeInfo typeManager = new ManagerQuestionTypeInfo();
    //ManagerCourseInfo courseInfoManager = new ManagerCourseInfo();
    //ManagerQuestionInfo questionManager = new ManagerQuestionInfo();

    ManagerPaper objManagerPaper = new ManagerPaper();
    Course objCourse = new Course();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();

    //多表查询
    const string SEARCH_QUESTION_QUESTIONTYPE_TCOURSE = "select q.QuestionDefaultScore,q.QuestionID as ID,q.QuestionContent,qt.QuestionTypeName,q.QuestionDiffculty,co.CourseName from Question q,QuestionType qt,TO_Course co where q.FKQuestionTypeID=qt.QuestionTypeID and co.Course_Id=q.FKQuestionCourseID {1} and q.QuestionID not in (select FKPaperQuestionTempQuestionID  from PaperQuestionTemp where FKPaperQuestionTempPaperID={0} )";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["SearchType"] = "0";  //0简单搜索1高级搜索

            string paperid = Request.QueryString["p"].ToString();
            //paperid = paperid;
            ViewState.Add("PaperID",paperid);
            BinderDLL();


            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            InitPage();
        }
    }

    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_SelectQuestion_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestionContent.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_Course.Text = GetLanageValueByKey("lbl_AddResourceInfo_CourseName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QuestonType.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionType", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_QeustionDiffculty.Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);

        chbox_all.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chbox_Unselect.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        btnNext.Text = GetLanageValueByKey("lab_SelectQuestion_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnPreview.Text = GetLanageValueByKey("btn_SelectQuestion_btnPreview", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSave.Text = GetLanageValueByKey("btn_SelectQuestion_SaveAndSelect", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnUp.Text = GetLanageValueByKey("btn_BuilderPaper_GoBack", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
        //lab_QuestionContent.Text = GetLanageValueByKey("btn_BuilderPaper_GoBack", PowerCreator.LMS.Language.LanagetItemType.Page);
        //txtQuestionContent.Text = GetLanageValueByKey("btn_BuilderPaper_GoBack", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_Course.Text = GetLanageValueByKey("btn_BuilderPaper_GoBack", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_QuestonType.Text = GetLanageValueByKey("btn_BuilderPaper_GoBack", PowerCreator.LMS.Language.LanagetItemType.Page);
        //lab_QeustionDiffculty.Text = GetLanageValueByKey("btn_BuilderPaper_GoBack", PowerCreator.LMS.Language.LanagetItemType.Page);



    }
    private string GetSearch()
    {
        StringBuilder search = new StringBuilder();
        string QuestionContent = txtQuestionContent.Text.Trim();
        string CourseName = txtCourseName.Text.Trim();
        string Diffculty = ddlDiffculty.SelectedValue;
        string QuestionTypeID = ddlQuestionTypeList.SelectedValue;
        search.Append(" and QuestionState=2 and FKQuestionCourseID!=-1");
        if (ViewState["SearchType"].ToString() == "1")
        {
            if (!string.IsNullOrEmpty(QuestionContent))
            {
                search.Append("  and  QuestionContent like '%" + QuestionContent + "%'");

            }
            if (!string.IsNullOrEmpty(CourseName))
            {
                search.Append(" and  co.CourseName like '%" + CourseName + "%'");

            }
            if (Diffculty != "null")
            {
                search.Append("  and  QuestionDiffculty =" + Diffculty);

            }
            if (QuestionTypeID != "null")
            {
                search.Append("  and  q.FKQuestionTypeID =" + QuestionTypeID);

            }
        }
        else
        {
            if (txt_SearchKey.Text.Trim()!="")
            {
                search.Append("  and  QuestionContent like '%" + txt_SearchKey.Text.Trim() + "%'");

            }
        }

       string str_search= string.Format(SEARCH_QUESTION_QUESTIONTYPE_TCOURSE, ViewState["PaperID"].ToString(), search.ToString());

       return str_search;
    }
    private void BinderPager(string search)
    {
        //AspNetPager1.RecordCount = questionManager.SelectQuestionListCount(search, int.Parse(ViewState["PaperID"].ToString()));
        AspNetPager1.RecordCount = objViewQuery.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        //dt = questionManager.SelectQuestionList(search,pageIndex, FBasePage.PageSize(), int.Parse(ViewState["PaperID"].ToString()));
        dt = objViewQuery.Query(search, "", LMSPageSize, pageIndex);
        dt.Columns.Add("QuestionContent_Clear");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["QuestionContent_Clear"] = StringUtils.GetSubstringLue(dt.Rows[i]["QuestionContent"].ToString(), 15);
        }
        gvQuestionList.DataSource = dt;
        gvQuestionList.DataBind();

        PreAspNetPager(AspNetPager1);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "1";
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);
    }

    private void BinderDLL()
    {

        DataTable dt = new DataTable();
        dt = objManagerQuestion.QueryQuestionType("");
        ddlQuestionTypeList.DataSource = dt;
        ddlQuestionTypeList.DataTextField = "QuestionTypeName";
        ddlQuestionTypeList.DataValueField = "QuestionTypeID";
        ddlQuestionTypeList.DataBind();
        ddlQuestionTypeList.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionTypeList.Items[1].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Panduanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionTypeList.Items[2].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Danxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionTypeList.Items[3].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Duoxuanti", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionTypeList.Items[4].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Tiankongti", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionTypeList.Items[5].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Mingcijieshiti", PowerCreator.LMS.Language.LanagetItemType.Page);
        ddlQuestionTypeList.Items[6].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Jiandati", PowerCreator.LMS.Language.LanagetItemType.Page);
        //ddlQuestionTypeList.Items[7].Text = GetLanageValueByKey("lab_EditUserPaper_Question_Zuheti", PowerCreator.LMS.Language.LanagetItemType.Page);

        ddlDiffculty.Items[0].Text = GetLanageValueByKey("Msg_ManagerQuestion_PleaseCheck", PowerCreator.LMS.Language.LanagetItemType.Page);
        
    }
    //private void GridBand(string QuestionContent, string CourseName, string Diffculty, string QuestionTypeID)
    //{
    //    //读取试题列表
    //    StringBuilder str = new StringBuilder();
    //    str.Append("select q.DefaultAnswer,q.QuestionID as ID,q.QuestionContent,qt.QuestionTypeName,q.Diffculty,DefaultSource,co.CourseName from QuestionInfo q,QuestionTypeInfo qt,CourseInfo co where q.QuestionTypeID=qt.QuestionTypeID and co.CourseID=q.CourseID");

    //    str.Append(" and q.QuestionID not in (select QuestionID from PaperQuestionInfo where PaperID=" + ViewState["PaperID"].ToString()+ "  )");
    //    if (!string.IsNullOrEmpty(QuestionContent))
    //    {
    //        str.Append("  and  QuestionContent like '%" + QuestionContent + "%'");

    //    }
    //    if (!string.IsNullOrEmpty(CourseName))
    //    {
    //        str.Append(" and  co.CourseName like '%" + CourseName + "%'");

    //    }
    //    if (Diffculty != "null")
    //    {
    //        str.Append("  and  Diffculty =" + Diffculty);

    //    }
    //    if (QuestionTypeID != "null")
    //    {
    //        str.Append("  and  q.QuestionTypeID =" + QuestionTypeID);

    //    }
    //    ViewState["queryString"] = str.ToString();
    //    gvQuestionList.DataSource = FPager.ExecutePager(Convert.ToInt32(ViewState["pageIndex"].ToString()), Convert.ToInt32(ViewState["pageSize"].ToString()), ViewState["showString"].ToString(), ViewState["queryString"].ToString(), ViewState["whereString"].ToString(), ViewState["orderString"].ToString(), ViewState["tabName"].ToString(), out pageCount, out recordCount, out dqpageCount);

    //    ViewState["pageCount"] = pageCount;
    //    ViewState["recordCount"] = recordCount;
    //    this.labRecordCount.Text = recordCount.ToString();
    //    this.labPageCount.Text = pageCount.ToString();
    //    this.lab_dqpageCount.Text = this.dqpageCount.ToString();
    //    gvQuestionList.DataBind();
    //    check();
    //}
       
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
        //将试题ID插入到表PaperQuestionInfo表中
        VOPaperQuestionTemp info = new VOPaperQuestionTemp();
        


        info.FKPaperQuestionTempPaperID = int.Parse(ViewState["PaperID"].ToString());
        info.PaperQuestionTempClassID = 0;
        //保存试题
        for (int i = 0; i < gvQuestionList.Rows.Count; i++)
        {
            CheckBox chk = (CheckBox)gvQuestionList.Rows[i].Cells[0].FindControl("chkIsSelected");
            if (chk.Checked)
            {
                Label lbl = (Label)gvQuestionList.Rows[i].Cells[0].FindControl("lblQuestionID");
                //Label lblAnswer = (Label)gvQuestionList.Rows[i].Cells[0].FindControl("lblAnswer");
                Label labQuestionContent = (Label)gvQuestionList.Rows[i].Cells[0].FindControl("labQuestionContent");

                info.FKPaperQuestionTempQuestionID = int.Parse(lbl.Text);
                //info.PaperQuestionTempOptionID = lblAnswer.Text;
                info.PaperQuestionTempScore = int.Parse(gvQuestionList.Rows[i].Cells[4].Text.ToString());
                info.PaperQuestionTempContent = labQuestionContent.Text;
                objManagerPaper.CreatePaperQuestionTemp(info);                
            }
        }
        Script.Alert(Page, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, 1);

    }
    protected void chbox_all_CheckedChanged(object sender, EventArgs e)
    {
        chbox_Unselect.Checked = false; 
        if (chbox_all.Checked == true)
        {
            
            for (int i = 0; i < gvQuestionList.Rows.Count; i++)
            {
                CheckBox objcheckbox = (CheckBox)gvQuestionList.Rows[i].Cells[0].FindControl("chkIsSelected");
                if (objcheckbox.Checked == false)
                {
                    objcheckbox.Checked = true;
                }
            }
        }
        else
        {
            for (int i = 0; i < gvQuestionList.Rows.Count; i++)
            {
                CheckBox objcheckbox = (CheckBox)gvQuestionList.Rows[i].Cells[0].FindControl("chkIsSelected");
                if (objcheckbox.Checked == true)
                {
                    objcheckbox.Checked = false;
                }
            }
        }

    }
    protected void chbox_Unselect_CheckedChanged(object sender, EventArgs e)
    {
        chbox_all.Checked = false;
        if (chbox_all.Checked == true)
        {
            chbox_Unselect.Checked = false;
        }
        for (int i = 0; i < gvQuestionList.Rows.Count; i++)
        {
            CheckBox objcheckbox = (CheckBox)gvQuestionList.Rows[i].Cells[0].FindControl("chkIsSelected");
            if (objcheckbox.Checked == true)
            {
                objcheckbox.Checked = false;
            }
            else
            {
                objcheckbox.Checked = true;
            }
        }
    }
    protected void btnPreview_Click(object sender, EventArgs e)
    {
        string url = "ShowPaperInfoTemp.aspx?p=" + ViewState["PaperID"].ToString();
        Script.NewOpenURL(url);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        string url = "BuilderPaper.aspx?p=" + ViewState["PaperID"].ToString()+"&type=1";
        Script.RedirectURL(url);
    }
    protected void btnUp_Click(object sender, EventArgs e)
    {
        string url = "EditPaperInfo.aspx?p=" +ViewState["PaperID"].ToString();
        Script.RedirectURL(url);
    }
    protected void gvQuestionList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionContent", PowerCreator.LMS.Language.LanagetItemType.Page);
            
            e.Row.Cells[2].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionType", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[4].Text = GetLanageValueByKey("Msg_ManagerQuestion_QuestionScore", PowerCreator.LMS.Language.LanagetItemType.Page);
            e.Row.Cells[3].Text = GetLanageValueByKey("lab_AddQuestionInfo_danx_QuestionDiffculty", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[8].Text = GetLanageValueByKey("btn_ManagerQuestion_QuestionState", PowerCreator.LMS.Language.LanagetItemType.Page);
            //e.Row.Cells[9].Text = GetLanageValueByKey("btn_ManagerQuestion_Work", PowerCreator.LMS.Language.LanagetItemType.Page);

        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");
        } 
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {
        ViewState["SearchType"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
        
    }
    protected void rptRole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
       
    }
    protected void gvQuestionList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        
        //if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        //{
        //    LinkButton lnkbtn_del1 = (LinkButton)e.Item.FindControl("lnkbtn_del");
        //    lnkbtn_del1.Attributes.Add("onClick", "javascript:return confirm('" + GetLanageValueByKey("lbl_OnlineUser_DelMsg", PowerCreator.LMS.Language.LanagetItemType.Page) + "')");

        //}
    }
}
